package com.zzw.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zzw.utils.*;
import com.zzw.bean.User;
import com.zzw.service.imple.IUserService;


@Controller
public class UserController {
	@Autowired
	IUserService userService;
	
	/**
	 * 转发页面
	 * */
	@RequestMapping("/userList.do")
	@ResponseBody
	public ModelAndView userList(User user) {
		ModelAndView mv=new ModelAndView();
		List<User> list=userService.selectUser(user);
		mv.addObject("list",list);
		mv.setViewName("user_list");
		return mv;
	}
	
	/**
	 * Ajax异步获取用户列表
	 * */
	@RequestMapping("/getUsers.do")
	@ResponseBody
	public Result getUsers(User user) {
		Result result=null;
		List<User> list=userService.selectUser(user);
		result=ResultUtils.success(list);
		return result;
	}
	
	/**
	 * 添加用户
	 * */
	@RequestMapping("addUser.do")
	@ResponseBody
	public Result addUser(User user) {
		Result result=null;
		userService.addUser(user);
		result=ResultUtils.success();
		return result;
	}
	
	/**
	 * 删除用户
	 * */
	@RequestMapping("deleteUser.do")
	@ResponseBody
	public Result deleteUser(Integer id) {
		Result result=null;
		userService.deleteUser(id);;
		result=ResultUtils.success();
		return result;
	}
	
	/**
	 * id查询单个用于用户
	 * */
	@RequestMapping("/selectUserById.do")
	@ResponseBody
	public Result selectUserById(Integer id) {
		Result result=null;
		User user=userService.selectUserById(id);
		result=ResultUtils.success(user);
		return result;
	}
	
	/**
	 * 更新单个用于用户
	 * */
	@RequestMapping("/updateUser.do")
	@ResponseBody
	public Result updateUser(User user) {
		Result result=null;
		userService.updateUser(user);
		result=ResultUtils.success();
		return result;
	}

}
